* control macros *
global ct_ind 		i.kor i.occupation i.teaor1_h1 ferfi mean_lag_w	// individual-level control variables (non-health) //
global ct_health 	log_health_12mo A_12mo-V_12mo			// individual-level control variables (health) //
global ct_FEi		FEi						// raw AKM individual FE //
global ct_FEi_imp	FEi_imp flag_missingFEi				// imputed AKM individual FE and its missing flag //
global ct_firm 		size_pre_imp FO_pre_imp logmean_w_pre_imp flag_missingsize_pre flag_missingFO_pre flag_missinglogmean_w_pre 	// firm-level control variables, corresponding to firm at t //
global ct_FEj		FEj_l4 FEj_l5 FEj_l6 FEj_l7 FEj_l8 FEj_l9 FEj_l10 FEj_l11 FEj_l12 					// raw AKM firm FE //
global ct_FEj_imp	flag_missingFEj_l4-flag_missingFEj_l12 FEj_l4_imp - FEj_l12_imp 		// imputed AKM firm FE and its missing flag //
global FE		i.d i.t						// d and time FE //
global FEt		i.t						// d and time FE //
global FEd		i.d						// d and time FE //

use "/homeKRTK/health_shocks/data_for_main_regs_event_study_20231203.dta", clear
drop if control==1
append using "/homeKRTK/health_shocks/additional_data_for_all_regs_controls_nod_20240422.dta"
append using "/homeKRTK/health_shocks/additional_data_for_IV_and_placebo_regs_20240416.dta"

replace treated=1 if d==2

gen NONEMP = 1 if ABS==1
	recode NONEMP (.=0) if ABS==0
	
set seed 12345

keep if d<=6

gen month_shock = tmerge_t
gen month_return = month_shock+d

keep anon t month_shock month_return 

sort anon 

save "/homeKRTK/health_shocks/jj_data_TLM_m.dta", replace

*Prep job-to-job transition indicator
global admin3 "/homeProspSSD/Admin3/admin3_alap.dta"
use anon t vallazon1  using $admin3, clear 

rename vallazon1 vallazon

merge m:1 vallazon using "/homeKRTK/health_shocks/AKM-FEs/FEj_full_Jan2023.dta", nogen keep(master match)

rename vallazon firmID
rename vall_fix firmFE
keep anon t firmID firmFE

merge m:1 anon using "/homeKRTK/health_shocks/jj_data_TLM_m.dta"
tab _m

keep if _m==3
drop _m

gen before = 1 if t < month_shock
gen after  = 1 if t > month_return
gen event  = 1 if t==month_shock 

gen relative_month = 0 if event==1
	replace relative_month = t-month_shock if before==1
	replace relative_month = t-month_return if after==1
	
* quarter= 
gen q = 8     if relative_month==-1 | relative_month==-2 | relative_month==-3
replace q = 16 if relative_month==22 | relative_month==23 | relative_month==24
replace q = 15 if relative_month==19 | relative_month==20 | relative_month==21
replace q = 14 if relative_month==16 | relative_month==17 | relative_month==18
replace q = 13 if relative_month==13 | relative_month==14 | relative_month==15
replace q = 12 if relative_month==10 | relative_month==11 | relative_month==12
replace q = 11 if relative_month==7 | relative_month==8 | relative_month==9
replace q = 10 if relative_month==4 | relative_month==5 | relative_month==6
replace q = 9 if relative_month==1 | relative_month==2 | relative_month==3
replace q = 7 if relative_month==-4 | relative_month==-5 | relative_month==-6
replace q = 6 if relative_month==-7 | relative_month==-8 | relative_month==-9
replace q = 5 if relative_month==-10 | relative_month==-11 | relative_month==-12
replace q = 4 if relative_month==-13 | relative_month==-14 | relative_month==-15
replace q = 3 if relative_month==-16 | relative_month==-17 | relative_month==-18
replace q = 2 if relative_month==-19 | relative_month==-20 | relative_month==-21
replace q = 1 if relative_month==-22 | relative_month==-23 | relative_month==-24

drop if q==.

save "/homeKRTK/health_shocks/jj_data_TLM1.dta", replace

use "/homeKRTK/health_shocks/jj_data_TLM1.dta", clear
* within quarter first observation:
egen first_m = min(t), by(anon q)
* within quarter last observation:
egen last_m  = max(t), by(anon q)
gen first_firmID_temp = firmID if t==first_m
gen last_firmID_temp  = firmID if t==last_m
gen first_firmFE_temp = firmFE if t==first_m
gen last_firmFE_temp  = firmFE if t==last_m
foreach x in first_firmID last_firmID first_firmFE last_firmFE {
	egen `x' = min(`x'_temp), by(anon q)
	drop `x'_temp
}

* within quarter avg FE *
egen mean_firmFE = mean(firmFE), by(anon q)

* 2 variables: switch up, switch down (WITHIN)
gen switch_up_w = 1 if (last_firmID!=first_firmID & last_firmID!=. & first_firmID!=.) & last_firmFE>first_firmFE & last_firmFE!=. & first_firmFE!=. // non-missing firmIDs and firm FEs, firmIDs differ
	recode switch_up_w (.=0)   if last_firmFE!=. & first_firmFE!=.
	
gen switch_down_w = 1 if (last_firmID!=first_firmID & last_firmID!=. & first_firmID!=.) & last_firmFE<first_firmFE & last_firmFE!=. & first_firmFE!=. // non-missing firmIDs and firm FEs, firmIDs differ
	recode switch_down_w (.=0) if last_firmFE!=. & first_firmFE!=.

keep anon q switch* first* last* mean_firmFE
duplicates drop anon q, force
save "/homeKRTK/health_shocks/jj_data_TLM.dta", replace

use "/homeKRTK/health_shocks/data_for_main_regs_event_study_20231203.dta", clear
drop if control==1

append using "/homeKRTK/health_shocks/additional_data_for_IV_and_placebo_regs_20240416.dta"
replace treated=1 if d==2
drop if d==1 & control==0

append using "/homeKRTK/health_shocks/additional_data_for_all_regs_controls_d_distrib_as_treated_20250319.dta"

gen NONEMP = 1 if ABS==1
	recode NONEMP (.=0) if ABS==0
	
set seed 12345

keep if d<=6
*keep if d>=3 & d<=6
*keep if NONEMP !=.

gen month_shock = t-12*int(t/12)
	recode month_shock (0=12)
gen month_return = month_shock+d

*keep if treated == 1 | control == 1

forval i=1/8 {
	local j=`i'+8
	rename mean_fq`i'_w w_q`j'
	replace mean_fq`i'_FEj_imp =. if flag_missingFEj_fq`i'==1
	rename mean_fq`i'_FEj_imp FE_q`j'


}
forval i=1/8 {
	local j=8-`i'
	rename mean_lq`i'_w w_q`j'
	replace mean_lq`i'_FEj_imp =. if flag_missingFEj_lq`i'==1
	rename mean_lq`i'_FEj_imp FE_q`j'
}


* reshape *
capture drop FEj_t4 FEj_t5 FEj_t6 FEj_t7 FEj_t8 FEj_t9 FEj_t10 FEj_t11 FEj_t12 
capture drop lnw_restr_t4 lnw_restr_t5 lnw_restr_t6 lnw_restr_t7 lnw_restr_t8 lnw_restr_t9 lnw_restr_t10 lnw_restr_t11 lnw_restr_t12 

reshape long w_q FE_q, i(anon) j(relative_time)


recode teaor1_h1 (-1=99) (.=100)

cap drop date
gen date=t+(relative_time-8)*3
replace date=t+d+(relative_time-9)*3 if relative_time>=8

rename kor kor_orig

rename ev ev_orig
rename t t_orig
rename date t

gen ev=floor((t-1)/12)+2003
merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_alap.dta", nogen keep(master match) keepusing(vallazon1 wtip1 wh1 feor1_h2 kor)
replace wh1=. if wh1>40
rename vallazon1 vallazon
merge m:1 vallazon ev using "/homeKRTK/health_shocks/firmquality.dta", nogen keep(master match)

merge 1:1 anon t using "/homeKRTK/nok_munka/Mental_health/admin3_passziv_ho.dta", nogen keepusing(passziv_ho)
replace passziv_ho=0 if passziv_ho==.

by anon, sort: egen full_d=min(d)
drop if full_d==. //drop anons who are never in our baseline sample

rename t date
rename t_orig t

by anon, sort: egen full_t=min(t)

gen sl_relevant1_=passziv_ho if date>=full_t & date<=full_t+1
by anon, sort: egen sl_relevant1=total(sl_relevant1_)
drop sl_relevant1_

drop if d==. //back to baseline+falsification sample
drop if d>6

keep if sl_relevant1>0 | control==1 //sample restriction!!!

cap drop tfp_wr_w
cap drop w_q_w
cap drop FE_q_w
winsor2 tfp_wr, cuts(1 99) by(ev)
winsor2 w_q, cuts(1 99) by(ev)
winsor2 FE_q, cuts(1 99) by(ev)

gen tr_relative_time=treated*relative_time
cap drop rtdum*
tabulate tr_relative_time, generate(rtdum)

*forval i=2/8 {
forval i=3/8 {
*	replace rtdum`i'=rtdum`i'-rtdum1
	replace rtdum`i'=rtdum`i'-rtdum2
}

gen after=0
replace after=1 if relative_time>=9 & relative_time<=12
replace after=2 if relative_time>=13 & relative_time<=16

xtset anon relative_time

save "/homeKRTK/health_shocks/analysis_data_20250329.dta", replace

use "/homeKRTK/health_shocks/analysis_data_20250329.dta", clear
cap drop rtdum*
tabulate tr_relative_time, generate(rtdum)
*forval i=2/8 {
forval i=3/8 {
*	replace rtdum`i'=rtdum`i'-rtdum1
	replace rtdum`i'=rtdum`i'-rtdum2
}

gen q = relative_time
replace q = relative_time+1 if relative_time<9 // for 0-7 we need to add 1 to match q // 

merge 1:1 anon q using "/homeKRTK/health_shocks/jj_data_TLM.dta"
drop _m

xtset anon q
sort anon q

gen switch_up_bFE = 1 if mean_firmFE>l.mean_firmFE & mean_firmFE!=. & l.mean_firmFE!=.
	recode switch_up_bFE (.=0) if mean_firmFE!=. & l.mean_firmFE!=.
gen switch_down_bFE = 1 if mean_firmFE<l.mean_firmFE & mean_firmFE!=. & l.mean_firmFE!=.
	recode switch_down_bFE (.=0) if mean_firmFE!=. & l.mean_firmFE!=.

	
****Input for Figure 8 - saved in "$folder\JJ_data.xlsx"	
log using /homeKRTK/health_shocks/EventStudy/JJ_JoLE.log, replace
estimates clear
xtreg switch_up_bFE i.date i.relative_time rtdum3-rtdum16 if ev_orig>=2012 & letszam~=. & FE_q~=., fe rob
	est store upb
	lincom -rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

xtreg switch_down_bFE i.date i.relative_time rtdum3-rtdum16 if ev_orig>=2012 & letszam~=. & FE_q~=., fe rob
	est store downb
	lincom rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

cap drop rtdum*
tabulate tr_relative_time, generate(rtdum)
forval i=2/8 {
	replace rtdum`i'=rtdum`i'-rtdum1
}

xtreg switch_up_w i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & letszam~=. & FE_q~=., fe rob
	est store upw
	lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

xtreg switch_down_w i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & letszam~=. & FE_q~=., fe rob
	est store downw
	lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

	
esttab *, cells(b(star fmt(5)) se(par([ ]) fmt(5)) t(par(( )) fmt(2))) legend nostar stats(r2 N, fmt(%9.3f %9.0g) labels(R^2))

log close